﻿import QtQuick 2.5

//通用按钮组件
Item {
    id: item_btn
    width: btn_bg.width;
    height: btn_bg.height;

    signal s_clickBtn();
    signal s_pressBtn();
    signal s_longPressBtn();//长压

    property alias selfImgObject: btn_bg;
    property alias selfIconObject: btn_icon;
    property alias selfTextObject: text;
    property alias selfMouseObject: mouse;
    property bool enabled: true;
    property bool selected: false;
    property int fontSize: 16

    onEnabledChanged: {
        if(enabled){
            btn_bg.source = btnImgStyle[_NORMAL_COLOR];
        }
        else{
            btn_bg.source = btnImgStyle[_DISABLED_COLOR];
        }
    }
    onSelectedChanged: {
        if(enabled){
            btn_bg.source = selected?btnImgStyle[_SELECTED_COLOR]:btnImgStyle[_NORMAL_COLOR];
        }
        else{
            btn_bg.source = btnImgStyle[_DISABLED_COLOR];
        }
    }

    Behavior on scale {
        NumberAnimation{duration: 100}
    }

    property var btnImgStyle: [
        "",   //正常颜色
        "",   //hover颜色
        "",   //press颜色
        "",   //选中颜色
        "",   //选中的hover颜色
        "",   //选中的press颜色
        ""    //置灰的颜色
    ]
    property int _NORMAL_COLOR:          0;
    property int _HOVER_COLOR:           1;
    property int _PRESS_COLOR:           2;
    property int _SELECTED_COLOR:        3;
    property int _SELECTED_HOVER_COLOR:  4;
    property int _SELECTED_PRESS_COLOR:  5;
    property int _DISABLED_COLOR:        6;

    onBtnImgStyleChanged: {
        btn_bg.source = btnImgStyle[_NORMAL_COLOR];
    }

    Image{
        id: btn_bg
//        width: parent.width
//        height: parent.height
//        source: parent.btnImgStyle[parent._NORMAL_COLOR];
    }
    Image{
        id: btn_icon
        width: parent.width
        height: parent.height
        opacity: 0
    }
    Text{
        id: text
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        color: "white";
        font.bold: false;
        font.pixelSize: fontSize;
        font.family: "微软雅黑"
        smooth: true;
        width: contentWidth
        height: contentHeight
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        clip: true
        wrapMode: Text.WordWrap
    }


    MouseArea{
        id: mouse
        anchors.fill: parent;
        hoverEnabled: true;
        enabled: item_btn.enabled;

//        property bool selected: false;
        property bool pressed: false;

        onEntered: {        
            btn_bg.source = selected ? btnImgStyle[_SELECTED_HOVER_COLOR]
                                     : btnImgStyle[_HOVER_COLOR];
        }
        onExited: {
            btn_bg.source = selected ? btnImgStyle[_SELECTED_COLOR]
                                     : btnImgStyle[_NORMAL_COLOR];
        }
        onPressed: {
            pressed = true;
            btn_bg.source = selected ? btnImgStyle[_SELECTED_PRESS_COLOR]
                                     : btnImgStyle[_PRESS_COLOR];

            item_btn.scale = 1.1
            s_pressBtn()
        }
        onReleased: {
            pressed = false;
            item_btn.scale = 1.0
            if(mouse.x>0 && mouse.x<parent.width && mouse.y>0 && mouse.y<parent.height){
                selected = !selected;
                s_clickBtn();
            }else{
            }
            if(item_btn.enabled){
                btn_bg.source = selected ? btnImgStyle[_SELECTED_COLOR]
                                         : btnImgStyle[_NORMAL_COLOR];
            }
            else{
                btn_bg.source = btnImgStyle[_DISABLED_COLOR]
            }
        }
    }
}
